@file:JvmName("OtherFunction")

package dsl

import expr.*

fun ifNull(query: Query, value: Query): Query {
    return QueryExprFunction("*IFNULL", listOf(query, value))
}

fun <T> ifNull(query: Query, value: T): Query {
    return ifNull(query, const(value))
}

fun cast(query: Query, type: String): Query {
    return QueryCast(query, type)
}

fun stringAgg(
    query: Query,
    separator: String,
    distinct: Boolean = false,
    vararg orderBy: OrderBy
): Query {
    return QueryAggFunction("*STRING_AGG", listOf(query, const(separator)), distinct = distinct, orderBy = orderBy.toList())
}

fun arrayAgg(
    query: Query,
    separator: String,
    distinct: Boolean = false,
    vararg orderBy: OrderBy
): Query {
    return QueryAggFunction("*ARRAY_AGG", listOf(query, const(separator)), distinct = distinct, orderBy = orderBy.toList())
}

fun findInSet(value: Query, query: Query): Query {
    return QueryExprFunction("*FIND_IN_SET", listOf(value, query))
}

fun findInSet(value: String, query: Query): Query {
    return findInSet(const(value), query)
}

fun jsonLength(query: Query): Query {
    return QueryExprFunction("*JSON_LENGTH", listOf(query))
}

fun concat(vararg query: Query): Query {
    return QueryExprFunction("*CONCAT", query.toList())
}

fun concatWs(separator: String, vararg query: Query): Query {
    listOf<Query>(const(separator)) + query.toList()
    return QueryExprFunction("*CONCAT_WS", listOf<Query>(const(separator)) + query.toList())
}